#!/bin/sh

# Note this is replaced by make install, not configure!
export MX_MODULE_DIR=@mx_module_dir@

set -e

if test ! -d ${MX_MODULE_DIR}; then
    echo "Something bad happened with the install script"
    echo "MX_DIR isn't pointing to a valid directory"
    exit
fi

#process arguments
for arg in "$@"; do
    case $arg in
	"--passive")
	    PASSIVE=1
	    ;;
	"--manual")
	    MANUAL=1
	    ;;
    esac
done

#create the files /etc/mx_mapper/{passive,mapper} if specified.
if test ! -d "/etc/mx_mapper"; then
    echo "/etc/mx_mapper didn't exist.  Creating it."
    mkdir /etc/mx_mapper && chmod 655 /etc/mx_mapper
fi
if [ "$PASSIVE" ]; then
    echo "Configuring a passive mapper"
    touch /etc/mx_mapper/passive && chmod 644 /etc/mx_mapper/passive
else
    rm -f /etc/mx_mapper/passive
fi
if [ "$MANUAL" ]; then
    echo "Configuring a manual mapper"
    echo "Mapper will not be started automatically!"
    touch /etc/mx_mapper/manual && chmod 644 /etc/mx_mapper/manual
else
    rm -f /etc/mx_mapper/manual
fi

devd_path=/usr/local/etc/devd

mkdir -p $devd_path

install ${MX_MODULE_DIR}/../etc/mx_devd.conf ${devd_path}/
/etc/rc.d/devd restart

mkdir -p "/usr/local/etc/rc.d/"

if test -d "/usr/local/etc/rc.d/"; then
    echo "Installing init script"
    cp ${MX_MODULE_DIR}/mx_start_stop /usr/local/etc/rc.d/mx
fi

